package com.songlanyun.modules.studyingDetail.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.studying.entity.Studying;
import com.songlanyun.modules.studying.service.StudyingService;
import com.songlanyun.modules.studyingDetail.entity.StudyingDetail;
import com.songlanyun.modules.studyingDetail.service.StudyingDetailService;
import com.songlanyun.modules.studyingType.entity.StudyingType;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Map;

@RestController
@RequestMapping("/admin/studyingDetail")
public class StudyingDetailController extends AbstractController {


    @Autowired
    private StudyingDetailService studyingDetailService;
    @Autowired
    private StudyingService studyingService;


    @GetMapping("/list")
//    @RequiresPermissions("base:studyingDetail:list")
    public R list(@RequestParam Map<String, Object> params) {
        if (ObjectUtil.isNull(params.get("studying"))) {
            throw new RRException("所属课程不能为空");
        }
        studyingService.loadById(Long.valueOf(params.get("studying").toString()),true);
        PageUtils page = studyingDetailService.queryPage(params);
        return R.ok().put("page", page);
    }


    @GetMapping("/{id}")
//    @RequiresPermissions("base:studyingDetail:info")
    public R info(@PathVariable("id") Long id) {
        StudyingDetail studyingDetail = studyingDetailService.loadById(id, true);
        return R.ok().put("data", studyingDetail);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
//    @RequiresPermissions("base:studyingDetail:add")
    public R save(@RequestBody StudyingDetail studyingDetail) {
        ValidatorUtils.validateEntity(studyingDetail);
        studyingDetail.setCreateBy(getUserId());
        studyingDetail.setUpdateBy(getUserId());
        StudyingDetail resstudying = studyingDetailService.add(studyingDetail);
        return R.ok().put("data", resstudying);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
//    @RequiresPermissions("base:studyingDetail:update")
    public R update(@RequestBody StudyingDetail studyingDetail) {
        if (ObjectUtil.isNull(studyingDetail.getId())) {
            throw new RRException("ID不能为空");
        }
        ValidatorUtils.validateEntity(studyingDetail);
        studyingDetail.setUpdateBy(getUserId());
        StudyingDetail resstudying = studyingDetailService.update(studyingDetail);
        return R.ok().put("data", resstudying);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("base:studyingDetail:delete")
    public R delete(@PathVariable("id") Long id) {
        studyingDetailService.removeById(id);
        return R.ok();
    }

    /**
     * 下架
     */
    @DeleteMapping("/disable/{id}")
//    @RequiresPermissions("base:headerStatus:delete")
    public R disable(@PathVariable("id") Long id) {
        StudyingDetail studyingDetail = studyingDetailService.getById(id);
        studyingDetail.setHasStatus(!studyingDetail.getHasStatus());
        studyingDetailService.updateById(studyingDetail);
        return R.ok();
    }
}
